package com.example.frenchconjugationquiz.data;

import java.util.ArrayList;
import java.util.List;

import com.example.frenchconjugationquiz.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ConjugationDataSource
{
	// Database fields
	private SQLiteDatabase database;
	private ConjugationDBHelper dbHelper;
	private String[] allColumns = { ConjugationDBHelper.COLUMN_ID, ConjugationDBHelper.COLUMN_SUBJECT, ConjugationDBHelper.COLUMN_VERB, ConjugationDBHelper.COLUMN_TENSE, ConjugationDBHelper.COLUMN_CONJUGATION };

	public final String[] Subjects;
	public final String[] Tenses;

	public ConjugationDataSource(Context context)
	{
		dbHelper = new ConjugationDBHelper(context);
		Subjects = context.getResources().getStringArray(R.array.subjects);
		Tenses = context.getResources().getStringArray(R.array.tenses);
	}

	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
	}

	public void close()
	{
		dbHelper.close();
	}

	public ConjugationModel createConjugationModel(String subject, String verb, String tense, String conjugation)
	{
		ContentValues values = new ContentValues();
		values.put(ConjugationDBHelper.COLUMN_SUBJECT, subject);
		values.put(ConjugationDBHelper.COLUMN_VERB, verb);
		values.put(ConjugationDBHelper.COLUMN_TENSE, tense);
		values.put(ConjugationDBHelper.COLUMN_CONJUGATION, conjugation);
		long insertId = 0;
		try
		{
			insertId = database.insertOrThrow(ConjugationDBHelper.TABLE_CONJUGATION, null, values);
		}
		catch (Exception e)
		{
			// failed, simply pass

			return null;
		}

		Cursor cursor = database.query(ConjugationDBHelper.TABLE_CONJUGATION, allColumns, ConjugationDBHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		ConjugationModel newmodel = cursorToConjugationModel(cursor);
		cursor.close();
		return newmodel;
	}

	public void deleteConjugation(ConjugationModel conjugation)
	{
		long id = conjugation.getId();
		System.out.println("Conjugation deleted with id: " + id);
		database.delete(ConjugationDBHelper.TABLE_CONJUGATION, ConjugationDBHelper.COLUMN_ID + " = " + id, null);
	}

	public List<ConjugationModel> getAllConjugationModels()
	{
		List<ConjugationModel> conjugations = new ArrayList<ConjugationModel>();

		Cursor cursor = database.query(ConjugationDBHelper.TABLE_CONJUGATION, allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast())
		{
			ConjugationModel conjugation = cursorToConjugationModel(cursor);
			conjugations.add(conjugation);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return conjugations;
	}

	private ConjugationModel cursorToConjugationModel(Cursor cursor)
	{
		ConjugationModel conjugation = new ConjugationModel();
		conjugation.setId(cursor.getLong(0));
		conjugation.setSubject(cursor.getString(1));
		conjugation.setVerb(cursor.getString(2));
		conjugation.setTense(cursor.getString(3));
		conjugation.setConjugation(cursor.getString(4));
		return conjugation;
	}

	public int getCount()
	{
		Cursor cursor = database.query(ConjugationDBHelper.TABLE_CONJUGATION, allColumns, null, null, null, null, null);
		return cursor.getCount();
	}

	public void clearData()
	{
		dbHelper.resetDb(database);
	}

	public ConjugationModel getRandomModel()
	{
		Cursor cursor = database.query(ConjugationDBHelper.TABLE_CONJUGATION, allColumns, null, null, null, null, "RANDOM() LIMIT 1");
		cursor.moveToFirst();
		return cursorToConjugationModel(cursor);
	}
}
